<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>环境编程代码实例：进程间通信（IPC） | 夜悊的技术小宅</title><meta name="author" content="夜悊"><meta name="copyright" content="夜悊"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="前言环境编程代码实例：进程间通信（IPC）。  代码仓库 yezhening&#x2F;Environment-and-network-programming-examples: 环境和网络编程实例 (github.com) Environment-and-network-programming-examples: 环境和网络编程实例 (gitee.com)   内容低级：3种  全局变量&amp;#x2">
<meta property="og:type" content="article">
<meta property="og:title" content="环境编程代码实例：进程间通信（IPC）">
<meta property="og:url" content="http://example.com/2023/05/16/%E7%8E%AF%E5%A2%83%E7%BC%96%E7%A8%8B%E4%BB%A3%E7%A0%81%E5%AE%9E%E4%BE%8B%EF%BC%9A%E8%BF%9B%E7%A8%8B%E9%97%B4%E9%80%9A%E4%BF%A1%EF%BC%88IPC%EF%BC%89/index.html">
<meta property="og:site_name" content="夜悊的技术小宅">
<meta property="og:description" content="前言环境编程代码实例：进程间通信（IPC）。  代码仓库 yezhening&#x2F;Environment-and-network-programming-examples: 环境和网络编程实例 (github.com) Environment-and-network-programming-examples: 环境和网络编程实例 (gitee.com)   内容低级：3种  全局变量&amp;#x2">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://example.com/img/cover3.png">
<meta property="article:published_time" content="2023-05-16T01:36:28.000Z">
<meta property="article:modified_time" content="2023-05-22T03:05:58.707Z">
<meta property="article:author" content="夜悊">
<meta property="article:tag" content="环境和网络编程">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://example.com/img/cover3.png"><link rel="shortcut icon" href="/img/favicon2.png"><link rel="canonical" href="http://example.com/2023/05/16/%E7%8E%AF%E5%A2%83%E7%BC%96%E7%A8%8B%E4%BB%A3%E7%A0%81%E5%AE%9E%E4%BE%8B%EF%BC%9A%E8%BF%9B%E7%A8%8B%E9%97%B4%E9%80%9A%E4%BF%A1%EF%BC%88IPC%EF%BC%89/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css?v=4.13.0"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.5.1/css/all.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui@5.0.33/dist/fancybox/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = {
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":true,"top_n_per_article":1,"unescape":false,"languages":{"hits_empty":"找不到您查询的内容：${query}","hits_stats":"共找到 ${hits} 篇文章"}},
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlight.js","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  dateSuffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: {"limitCount":50,"languages":{"author":"作者: 夜悊","link":"链接: ","source":"来源: 夜悊的技术小宅","info":"著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"}},
  lightbox: 'fancybox',
  Snackbar: undefined,
  infinitegrid: {
    js: 'https://cdn.jsdelivr.net/npm/@egjs/infinitegrid@4.11.1/dist/infinitegrid.min.js',
    buttonText: '加载更多'
  },
  isPhotoFigcaption: false,
  islazyload: true,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  },
  autoDarkmode: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '环境编程代码实例：进程间通信（IPC）',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2023-05-22 11:05:58'
}</script><script>(win=>{
      win.saveToLocal = {
        set: (key, value, ttl) => {
          if (ttl === 0) return
          const now = Date.now()
          const expiry = now + ttl * 86400000
          const item = {
            value,
            expiry
          }
          localStorage.setItem(key, JSON.stringify(item))
        },
      
        get: key => {
          const itemStr = localStorage.getItem(key)
      
          if (!itemStr) {
            return undefined
          }
          const item = JSON.parse(itemStr)
          const now = Date.now()
      
          if (now > item.expiry) {
            localStorage.removeItem(key)
            return undefined
          }
          return item.value
        }
      }
    
      win.getScript = (url, attr = {}) => new Promise((resolve, reject) => {
        const script = document.createElement('script')
        script.src = url
        script.async = true
        script.onerror = reject
        script.onload = script.onreadystatechange = function() {
          const loadState = this.readyState
          if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
          script.onload = script.onreadystatechange = null
          resolve()
        }

        Object.keys(attr).forEach(key => {
          script.setAttribute(key, attr[key])
        })

        document.head.appendChild(script)
      })
    
      win.getCSS = (url, id = false) => new Promise((resolve, reject) => {
        const link = document.createElement('link')
        link.rel = 'stylesheet'
        link.href = url
        if (id) link.id = id
        link.onerror = reject
        link.onload = link.onreadystatechange = function() {
          const loadState = this.readyState
          if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
          link.onload = link.onreadystatechange = null
          resolve()
        }
        document.head.appendChild(link)
      })
    
      win.activateDarkMode = () => {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = () => {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
        if (t === 'dark') activateDarkMode()
        else if (t === 'light') activateLightMode()
      
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
      const detectApple = () => {
        if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
          document.documentElement.classList.add('apple')
        }
      }
      detectApple()
    })(window)</script><link rel="stylesheet" href="/css/my_css.css"><meta name="generator" content="Hexo 7.3.0"></head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src= "" data-lazy-src="/img/avatar.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">92</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">15</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">15</div></a></div><hr class="custom-hr"/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fa fa-bookmark"></i><span> 笔记目录</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/categories/"><i class="fa-fw fa fa-folder-open"></i><span> 分类</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/archives/"><i class="fa-fw fa fa-archive"></i><span> 归档</span></a></li></ul></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fa fa-blog"></i><span> 其他博客</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="https://blog.csdn.net/m0_62083249"><i class="fa-fw fa fa-link"></i><span> CSDN</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://www.zhihu.com/people/ye-zhe-ning"><i class="fa-fw fa fa-link"></i><span> 知乎</span></a></li></ul></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fa fa-sitemap"></i><span> 代码仓库</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="https://gitee.com/yezhening"><i class="fa-fw fa fa-link"></i><span> Gitee</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://github.com/yezhening"><i class="fa-fw fa fa-link"></i><span> Github</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/about_me/"><i class="fa-fw fas fa-user"></i><span> 关于我</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/img/cover3.png')"><nav id="nav"><span id="blog-info"><a href="/" title="夜悊的技术小宅"><span class="site-name">夜悊的技术小宅</span></a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search" href="javascript:void(0);"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fa fa-bookmark"></i><span> 笔记目录</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/categories/"><i class="fa-fw fa fa-folder-open"></i><span> 分类</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fa fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/archives/"><i class="fa-fw fa fa-archive"></i><span> 归档</span></a></li></ul></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fa fa-blog"></i><span> 其他博客</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="https://blog.csdn.net/m0_62083249"><i class="fa-fw fa fa-link"></i><span> CSDN</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://www.zhihu.com/people/ye-zhe-ning"><i class="fa-fw fa fa-link"></i><span> 知乎</span></a></li></ul></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fa fa-sitemap"></i><span> 代码仓库</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" target="_blank" rel="noopener" href="https://gitee.com/yezhening"><i class="fa-fw fa fa-link"></i><span> Gitee</span></a></li><li><a class="site-page child" target="_blank" rel="noopener" href="https://github.com/yezhening"><i class="fa-fw fa fa-link"></i><span> Github</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/about_me/"><i class="fa-fw fas fa-user"></i><span> 关于我</span></a></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">环境编程代码实例：进程间通信（IPC）</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2023-05-16T01:36:28.000Z" title="发表于 2023-05-16 09:36:28">2023-05-16</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2023-05-22T03:05:58.707Z" title="更新于 2023-05-22 11:05:58">2023-05-22</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E7%8E%AF%E5%A2%83%E5%92%8C%E7%BD%91%E7%BB%9C%E7%BC%96%E7%A8%8B/">环境和网络编程</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="环境编程代码实例：进程间通信（IPC）"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>环境编程代码实例：进程间通信（IPC）。</p>
<hr>
<h1 id="代码仓库"><a href="#代码仓库" class="headerlink" title="代码仓库"></a>代码仓库</h1><ul>
<li><a target="_blank" rel="noopener" href="https://github.com/yezhening/Environment-and-network-programming-examples">yezhening&#x2F;Environment-and-network-programming-examples: 环境和网络编程实例 (github.com)</a></li>
<li><a target="_blank" rel="noopener" href="https://gitee.com/yezhening/Environment-and-network-programming-examples">Environment-and-network-programming-examples: 环境和网络编程实例 (gitee.com)</a></li>
</ul>
<hr>
<h1 id="内容"><a href="#内容" class="headerlink" title="内容"></a>内容</h1><p>低级：3种</p>
<ul>
<li>全局变量&#x2F;glov：类似管道、命名管道、消息队列、共享内存：头文件&#x2F;库文件全局变量，源文件全局变量</li>
<li>文件&#x2F;file：类似管道、命名管道、消息队列、共享内存</li>
<li>PV操作&#x2F;pv：类似信号量</li>
</ul>
<p>第三方：2种</p>
<ul>
<li>数据库&#x2F;db：类似管道、命名管道、消息队列、共享内存</li>
<li>缓存&#x2F;cache：类似管道、命名管道、消息队列、共享内存</li>
</ul>
<p>一般：7种</p>
<ul>
<li>管道&#x2F;无名管道&#x2F;pipe</li>
<li>命名管道&#x2F;有名管道&#x2F;FIFO</li>
<li>信号&#x2F;signal</li>
<li>信号量&#x2F;semaphore：XSI信号量，POSIX信号量</li>
<li>消息队列&#x2F;mq</li>
<li>共享内存&#x2F;shm</li>
<li>套接字&#x2F;socket</li>
</ul>
<p>高级：1种</p>
<ul>
<li>内存映射&#x2F;mmap：类似共享内存</li>
</ul>
<hr>
<h2 id="全局变量"><a href="#全局变量" class="headerlink" title="全局变量"></a>全局变量</h2><h3 id="头文件全局变量"><a href="#头文件全局变量" class="headerlink" title="头文件全局变量"></a>头文件全局变量</h3><p>缺点：</p>
<ul>
<li>只读，不可写</li>
<li>同步互斥问题：无法解决。需要引入其他进程间通信机制才能解决</li>
<li>扩展问题：无法解决。需要引入其他进程间通信机制才能解决</li>
</ul>
<p>相关语句：</p>
<ul>
<li>extern</li>
<li>#include</li>
<li>…</li>
</ul>
<hr>
<h3 id="源文件全局变量"><a href="#源文件全局变量" class="headerlink" title="源文件全局变量"></a>源文件全局变量</h3><p>缺点：</p>
<ul>
<li>只读，不可写</li>
<li>只能用于具有公共祖先的进程间</li>
<li>同步互斥问题：无法解决。需要引入其他进程间通信机制才能解决</li>
<li>扩展问题：无法解决。需要引入其他进程间通信机制才能解决</li>
</ul>
<hr>
<h2 id="文件"><a href="#文件" class="headerlink" title="文件"></a>文件</h2><p>缺点：</p>
<ul>
<li>性能问题：访问文件效率低</li>
<li>同步互斥问题：无法解决。需要引入其他进程间通信机制才能解决</li>
<li>扩展问题：无法解决。需要引入其他进程间通信机制才能解决</li>
</ul>
<p>相关函数：</p>
<ul>
<li>open()</li>
<li>…</li>
<li>fopen()</li>
<li>…</li>
</ul>
<hr>
<h2 id="PV操作"><a href="#PV操作" class="headerlink" title="PV操作"></a>PV操作</h2><p>缺点：</p>
<ul>
<li>同步互斥问题：无法解决。需要引入其他进程间通信机制才能解决</li>
<li>扩展问题：无法解决。需要引入其他进程间通信机制才能解决</li>
</ul>
<hr>
<h2 id="数据库"><a href="#数据库" class="headerlink" title="数据库"></a>数据库</h2><p>优点：</p>
<ul>
<li>相对于文件，由相关机制优化同步互斥、性能和扩展问题。</li>
</ul>
<p>相关数据库：</p>
<ul>
<li>MySQL</li>
<li>SQL Server</li>
<li>…</li>
</ul>
<hr>
<h2 id="缓存"><a href="#缓存" class="headerlink" title="缓存"></a>缓存</h2><p>优点：</p>
<ul>
<li>相对于文件，由相关机制优化同步互斥、性能和扩展问题。</li>
</ul>
<p>相关缓存：</p>
<ul>
<li>Memcached</li>
<li>Redis</li>
<li>…</li>
</ul>
<hr>
<h2 id="管道"><a href="#管道" class="headerlink" title="管道"></a>管道</h2><p>缺点：</p>
<ul>
<li>半双工</li>
<li>只能用于具有公共祖先的进程间</li>
</ul>
<p>相关函数：</p>
<ul>
<li>pipe()：创建管道</li>
<li>popen()：打开管道</li>
<li>pclose()：关闭管道</li>
</ul>
<hr>
<h2 id="命名管道"><a href="#命名管道" class="headerlink" title="命名管道"></a>命名管道</h2><p>相关函数：</p>
<ul>
<li>mkfifo()：创建命名管道</li>
<li>mkfifoat()：创建命名管道</li>
</ul>
<hr>
<h2 id="信号"><a href="#信号" class="headerlink" title="信号"></a>信号</h2><p>相关函数：</p>
<ul>
<li>signal()</li>
<li>kill()</li>
<li>…</li>
</ul>
<hr>
<h2 id="信号量"><a href="#信号量" class="headerlink" title="信号量"></a>信号量</h2><h3 id="XSI信号量"><a href="#XSI信号量" class="headerlink" title="XSI信号量"></a>XSI信号量</h3><p>缺点：</p>
<ul>
<li>在系统范围内起作用，没有引用计数。不能隐式删除，需要显式删除</li>
<li>在文件系统中没有名字。不能使用文件相关函数操作，需要使用特定的系统调用或命令</li>
<li>不使用文件描述符。不能使用多路复用函数操作，一般一次只使用一个而不是多个（消息队列）</li>
</ul>
<p>相关函数：</p>
<ul>
<li>semget()：获取信号量</li>
<li>semctl()：控制信号量</li>
<li>semop()：操作信号量</li>
</ul>
<hr>
<h3 id="POSIX信号量"><a href="#POSIX信号量" class="headerlink" title="POSIX信号量"></a>POSIX信号量</h3><p>相关函数：</p>
<ul>
<li>sem_open()：打开有名信号量</li>
<li>sem_close()：关闭信号量</li>
<li>sem_unlink()：销毁有名信号量</li>
<li>sem_trywait()：尝试等待信号量</li>
<li>sem_wait()：等待信号量</li>
<li>sem_timewait()：定时等待信号量</li>
<li>sem_post()：提交信号量</li>
<li>sem_init()：初始化无名信号量</li>
<li>sem_destroy()：销毁无名信号量</li>
<li>sem_getvalue()：获取信号量值</li>
</ul>
<hr>
<h2 id="消息队列-mq"><a href="#消息队列-mq" class="headerlink" title="消息队列&#x2F;mq"></a>消息队列&#x2F;mq</h2><p>缺点：</p>
<ul>
<li>在系统范围内起作用，没有引用计数。不能隐式删除，需要显式删除</li>
<li>在文件系统中没有名字。不能使用文件相关函数操作，需要使用特定的系统调用或命令</li>
<li>不使用文件描述符。不能使用多路复用函数操作，一般一次只使用一个而不是多个（消息队列）</li>
</ul>
<p>相关函数：</p>
<ul>
<li>ftok()：创建键</li>
<li>msgget()：获取消息队列</li>
<li>msgctl()：控制消息队列</li>
<li>msgsnd()：发送消息到消息队列</li>
<li>msgrcv()：接收消息从消息队列</li>
</ul>
<hr>
<h2 id="共享内存-shm"><a href="#共享内存-shm" class="headerlink" title="共享内存&#x2F;shm"></a>共享内存&#x2F;shm</h2><p>缺点：</p>
<ul>
<li>在系统范围内起作用，没有引用计数。不能隐式删除，需要显式删除</li>
<li>在文件系统中没有名字。不能使用文件相关函数操作，需要使用特定的系统调用或命令</li>
<li>不使用文件描述符。不能使用多路复用函数操作，一般一次只使用一个而不是多个（消息队列）</li>
</ul>
<p>相关函数：</p>
<ul>
<li>shmget()：获取共享内存</li>
<li>shmctl()：控制共享内存</li>
<li>shmat()：连接共享内存</li>
<li>shmdt()：分离共享内存</li>
</ul>
<hr>
<h2 id="套接字-sock"><a href="#套接字-sock" class="headerlink" title="套接字&#x2F;sock"></a>套接字&#x2F;sock</h2><p>优点：</p>
<ul>
<li>可用于不同主机进程间的通信</li>
</ul>
<p>相关函数：</p>
<ul>
<li>socket()</li>
<li>bind()</li>
<li>…</li>
</ul>
<hr>
<h2 id="内存映射"><a href="#内存映射" class="headerlink" title="内存映射"></a>内存映射</h2><p>相关函数：</p>
<ul>
<li>mmap()</li>
<li>munmap()</li>
</ul>
<hr>
<h1 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h1><h2 id="说明"><a href="#说明" class="headerlink" title="说明"></a>说明</h2><ul>
<li>由于代码篇幅较多，在博客中直接粘贴并呈现会显得冗长，因此未在博客中放置代码。请前往”代码仓库”查看或获取相关代码</li>
<li>对大部分总结的进程间通信机制提供简单实现</li>
<li>有详细步骤解析</li>
<li>只简单演示基础的操作，未考虑深层次的问题</li>
<li>对相关函数获取返回值进行错误处理，不相关函数不进行错误处理</li>
</ul>
<hr>
<h2 id="目录结构"><a href="#目录结构" class="headerlink" title="目录结构"></a>目录结构</h2><ul>
<li>头文件全局变量：glov_h</li>
<li>源文件全局变量：glov_s</li>
<li>文件，PV操作：file_pv</li>
<li>管道：pipe</li>
<li>命名管道：fifo</li>
<li>信号：sig</li>
<li>XSI信号量：sem_XSI</li>
<li>POSIX信号量：sem_POSIX</li>
<li>消息队列：mq</li>
<li>共享内存：shm</li>
<li>套接字：socket（使用代码仓库中，“传输控制协议（TCP）简单版：TCP_simple”）</li>
<li>内存映射：mmap</li>
</ul>
<hr>
<h1 id="持续更新"><a href="#持续更新" class="headerlink" title="持续更新"></a>持续更新</h1><ul>
<li>当前内容总结于博主和《UNIX环境高级编程（第3版）》的一部分</li>
<li>在《UNIX网络编程（第3版）卷2：进程间通信》中，还有其他的进程间通信机制，在后面有时间阅读后，持续更新代码仓库和博客</li>
</ul>
<hr>
<h1 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h1><p>环境编程代码实例：进程间通信（IPC）。</p>
<hr>
<h1 id="参考资料"><a href="#参考资料" class="headerlink" title="参考资料"></a>参考资料</h1><ul>
<li>《UNIX环境高级编程（第3版）》作者：W.Richard Stevens，Stephen A.Rago</li>
<li>《UNIX网络编程（第3版）》作者：W.Richard Stevens，Bill Fenner，Andrew M.Rudoff</li>
</ul>
<hr>
<h1 id="作者的话"><a href="#作者的话" class="headerlink" title="作者的话"></a>作者的话</h1><ul>
<li><strong>感谢参考资料的作者&#x2F;博主</strong></li>
<li>作者：夜悊</li>
<li>版权所有，转载请注明出处，谢谢~</li>
<li><strong>如果文章对你有帮助，请点个赞或加个粉丝吧，你的支持就是作者的动力~</strong></li>
<li>文章在描述时有疑惑的地方，请留言，定会一一耐心讨论、解答</li>
<li>文章在认识上有错误的地方, 敬请批评指正</li>
<li>望读者们都能有所收获</li>
</ul>
<hr>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta"><i class="fas fa-circle-user fa-fw"></i>文章作者: </span><span class="post-copyright-info"><a href="http://example.com">夜悊</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta"><i class="fas fa-square-arrow-up-right fa-fw"></i>文章链接: </span><span class="post-copyright-info"><a href="http://example.com/2023/05/16/%E7%8E%AF%E5%A2%83%E7%BC%96%E7%A8%8B%E4%BB%A3%E7%A0%81%E5%AE%9E%E4%BE%8B%EF%BC%9A%E8%BF%9B%E7%A8%8B%E9%97%B4%E9%80%9A%E4%BF%A1%EF%BC%88IPC%EF%BC%89/">http://example.com/2023/05/16/环境编程代码实例：进程间通信（IPC）/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta"><i class="fas fa-circle-exclamation fa-fw"></i>版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://example.com" target="_blank">夜悊的技术小宅</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/%E7%8E%AF%E5%A2%83%E5%92%8C%E7%BD%91%E7%BB%9C%E7%BC%96%E7%A8%8B/">环境和网络编程</a></div><div class="post_share"><div class="social-share" data-image="/img/cover3.png" data-sites="qq,wechat,weibo"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1.1.3/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1.1.3/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2023/05/12/%E7%BD%91%E7%BB%9C%E7%BC%96%E7%A8%8B%E4%BB%A3%E7%A0%81%E5%AE%9E%E4%BE%8B%EF%BC%9A%E5%AE%88%E6%8A%A4%E8%BF%9B%E7%A8%8B%E7%89%88/" title="网络编程代码实例：守护进程版"><img class="cover" src= "" data-lazy-src="/img/cover14.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">网络编程代码实例：守护进程版</div></div></a></div><div class="next-post pull-right"><a href="/2023/09/28/C++%E4%BB%A3%E7%A0%81%E7%A4%BA%E4%BE%8B%EF%BC%9A%E7%BB%84%E5%90%88%E6%95%B0%E7%AE%80%E5%8D%95%E7%94%9F%E6%88%90%E5%B7%A5%E5%85%B7/" title="C++代码示例：组合数简单生成工具"><img class="cover" src= "" data-lazy-src="/img/cover6.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">C++代码示例：组合数简单生成工具</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2022/04/16/IRC%EF%BC%88Internet%20Relay%20Chat%EF%BC%89%EF%BC%88%E5%9B%A0%E7%89%B9%E7%BD%91%E4%B8%AD%E7%BB%A7%E8%81%8A%E5%A4%A9%EF%BC%89%E5%8D%8F%E8%AE%AE%E2%80%94%E2%80%94RFC1459%E6%96%87%E6%A1%A3%E8%A6%81%E7%82%B9%E6%80%BB%E7%BB%93/" title="IRC（Internet Relay Chat）（因特网中继聊天）协议——RFC1459文档要点总结"><img class="cover" src= "" data-lazy-src="/img/cover4.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-04-16</div><div class="title">IRC（Internet Relay Chat）（因特网中继聊天）协议——RFC1459文档要点总结</div></div></a></div><div><a href="/2023/10/25/%E7%8E%AF%E5%A2%83%E7%BC%96%E7%A8%8B%E4%BB%A3%E7%A0%81%E5%AE%9E%E4%BE%8B%EF%BC%9A%E2%80%9C%E5%8A%A0_%E5%8D%B8%E8%BD%BD%E2%80%9D%20Zend%20Framework%202/" title="环境编程代码实例：“加&#x2F;卸载” Zend Framework 2"><img class="cover" src= "" data-lazy-src="/img/cover10.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-10-25</div><div class="title">环境编程代码实例：“加&#x2F;卸载” Zend Framework 2</div></div></a></div><div><a href="/2023/05/01/%E7%BD%91%E7%BB%9C%E7%BC%96%E7%A8%8B%E4%BB%A3%E7%A0%81%E5%AE%9E%E4%BE%8B%EF%BC%9AIO%E5%A4%8D%E7%94%A8%E7%89%88/" title="网络编程代码实例：IO复用版"><img class="cover" src= "" data-lazy-src="/img/cover16.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-05-01</div><div class="title">网络编程代码实例：IO复用版</div></div></a></div><div><a href="/2023/04/24/%E7%BD%91%E7%BB%9C%E7%BC%96%E7%A8%8B%E4%BB%A3%E7%A0%81%E5%AE%9E%E4%BE%8B%EF%BC%9A%E4%BC%A0%E8%BE%93%E6%8E%A7%E5%88%B6%E5%8D%8F%E8%AE%AE%EF%BC%88TCP%EF%BC%89%E7%AE%80%E5%8D%95%E7%89%88/" title="网络编程代码实例：传输控制协议（TCP）简单版"><img class="cover" src= "" data-lazy-src="/img/cover19.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-04-24</div><div class="title">网络编程代码实例：传输控制协议（TCP）简单版</div></div></a></div><div><a href="/2023/04/26/%E7%BD%91%E7%BB%9C%E7%BC%96%E7%A8%8B%E4%BB%A3%E7%A0%81%E5%AE%9E%E4%BE%8B%EF%BC%9A%E5%A4%9A%E8%BF%9B%E7%A8%8B%E7%89%88/" title="网络编程代码实例：多进程版"><img class="cover" src= "" data-lazy-src="/img/cover11.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-04-26</div><div class="title">网络编程代码实例：多进程版</div></div></a></div><div><a href="/2023/04/25/%E7%BD%91%E7%BB%9C%E7%BC%96%E7%A8%8B%E4%BB%A3%E7%A0%81%E5%AE%9E%E4%BE%8B%EF%BC%9A%E7%94%A8%E6%88%B7%E6%95%B0%E6%8D%AE%E6%8A%A5%E5%8D%8F%E8%AE%AE%EF%BC%88UDP%EF%BC%89%E7%AE%80%E5%8D%95%E7%89%88/" title="网络编程代码实例：用户数据报协议（UDP）简单版"><img class="cover" src= "" data-lazy-src="/img/cover2.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2023-04-25</div><div class="title">网络编程代码实例：用户数据报协议（UDP）简单版</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content is-expand"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%89%8D%E8%A8%80"><span class="toc-number">1.</span> <span class="toc-text">前言</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BB%A3%E7%A0%81%E4%BB%93%E5%BA%93"><span class="toc-number">2.</span> <span class="toc-text">代码仓库</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%86%85%E5%AE%B9"><span class="toc-number">3.</span> <span class="toc-text">内容</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%85%A8%E5%B1%80%E5%8F%98%E9%87%8F"><span class="toc-number">3.1.</span> <span class="toc-text">全局变量</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%A4%B4%E6%96%87%E4%BB%B6%E5%85%A8%E5%B1%80%E5%8F%98%E9%87%8F"><span class="toc-number">3.1.1.</span> <span class="toc-text">头文件全局变量</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%BA%90%E6%96%87%E4%BB%B6%E5%85%A8%E5%B1%80%E5%8F%98%E9%87%8F"><span class="toc-number">3.1.2.</span> <span class="toc-text">源文件全局变量</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%96%87%E4%BB%B6"><span class="toc-number">3.2.</span> <span class="toc-text">文件</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#PV%E6%93%8D%E4%BD%9C"><span class="toc-number">3.3.</span> <span class="toc-text">PV操作</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%95%B0%E6%8D%AE%E5%BA%93"><span class="toc-number">3.4.</span> <span class="toc-text">数据库</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%BC%93%E5%AD%98"><span class="toc-number">3.5.</span> <span class="toc-text">缓存</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%AE%A1%E9%81%93"><span class="toc-number">3.6.</span> <span class="toc-text">管道</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%91%BD%E5%90%8D%E7%AE%A1%E9%81%93"><span class="toc-number">3.7.</span> <span class="toc-text">命名管道</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BF%A1%E5%8F%B7"><span class="toc-number">3.8.</span> <span class="toc-text">信号</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BF%A1%E5%8F%B7%E9%87%8F"><span class="toc-number">3.9.</span> <span class="toc-text">信号量</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#XSI%E4%BF%A1%E5%8F%B7%E9%87%8F"><span class="toc-number">3.9.1.</span> <span class="toc-text">XSI信号量</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#POSIX%E4%BF%A1%E5%8F%B7%E9%87%8F"><span class="toc-number">3.9.2.</span> <span class="toc-text">POSIX信号量</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%B6%88%E6%81%AF%E9%98%9F%E5%88%97-mq"><span class="toc-number">3.10.</span> <span class="toc-text">消息队列&#x2F;mq</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%85%B1%E4%BA%AB%E5%86%85%E5%AD%98-shm"><span class="toc-number">3.11.</span> <span class="toc-text">共享内存&#x2F;shm</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%A5%97%E6%8E%A5%E5%AD%97-sock"><span class="toc-number">3.12.</span> <span class="toc-text">套接字&#x2F;sock</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%86%85%E5%AD%98%E6%98%A0%E5%B0%84"><span class="toc-number">3.13.</span> <span class="toc-text">内存映射</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BB%A3%E7%A0%81"><span class="toc-number">4.</span> <span class="toc-text">代码</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%AF%B4%E6%98%8E"><span class="toc-number">4.1.</span> <span class="toc-text">说明</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%9B%AE%E5%BD%95%E7%BB%93%E6%9E%84"><span class="toc-number">4.2.</span> <span class="toc-text">目录结构</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E6%8C%81%E7%BB%AD%E6%9B%B4%E6%96%B0"><span class="toc-number">5.</span> <span class="toc-text">持续更新</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E6%80%BB%E7%BB%93"><span class="toc-number">6.</span> <span class="toc-text">总结</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%8F%82%E8%80%83%E8%B5%84%E6%96%99"><span class="toc-number">7.</span> <span class="toc-text">参考资料</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E4%BD%9C%E8%80%85%E7%9A%84%E8%AF%9D"><span class="toc-number">8.</span> <span class="toc-text">作者的话</span></a></li></ol></div></div></div></div></main><footer id="footer" style="background-image: url('/img/cover3.png')"><div id="footer-wrap"><div class="copyright">&copy;2023 - 2025 By 夜悊</div><div class="footer_custom_text"><a target="_blank" href="https://beian.miit.gov.cn/" >琼ICP备2023001225号-1</a><br><img src= "" data-lazy-src="/img/beian.png"/>&nbsp<a target="_blank" href="https://www.beian.gov.cn/portal/registerSystemInfo?recordcode=46010802001221" >琼公网安备 46010802001221号</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="translateLink" type="button" title="简繁转换">繁</button><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button></div><div id="rightside-config-show"><button id="rightside-config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js?v=4.13.0"></script><script src="/js/main.js?v=4.13.0"></script><script src="/js/tw_cn.js?v=4.13.0"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui@5.0.33/dist/fancybox/fancybox.umd.min.js"></script><script src="https://cdn.jsdelivr.net/npm/vanilla-lazyload@17.8.8/dist/lazyload.iife.min.js"></script><div class="js-pjax"></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div><div id="local-search-stats-wrap"></div></div></div><div id="search-mask"></div><script src="/js/search/local-search.js?v=4.13.0"></script></div></div></body></html>